﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RushBuyAdditionPage : Page
    {
        public RushBuyAdditionItem threeStarItem;
        public RushBuyAdditionItem sixStarItem;
        public RushBuyAdditionItem nineStarItem;

        public UILabel curCostStarNumLabel;

        public UILabel hpAddLabel;
        public UILabel attAddLabel;
        public UILabel phyDefAddLabel;
        public UILabel magdefAddLabel;
        public UILabel hitAddLabel;
        public UILabel dodgeAddLabel;
        public UILabel criAddLabel;
        public UILabel antiCriAddLabel;
        public UILabel damageAddLabel;
        public UILabel damageReduceLabel;

        int curCostStarNum;

        protected override void DoOpen()
        {
            Refresh(StaticData.sectionAddition);
        }

        protected override void DoClose()
        {
            StaticData.sectionAddition = null;
        }

        void Refresh(ProtoBuf.Message.SCRushGetSectionAddition msg) {
            currentSelectItem = null;
            curCostStarNum = msg.costStar;
            curCostStarNumLabel.text = msg.costStar.ToString();
            threeStarItem.Init(msg.starResult3, 3, false);
            sixStarItem.Init(msg.starResult6, 6, false);
            nineStarItem.Init(msg.starResult9, 9, false);
            Refresh(msg.prop);
        }

        void Refresh(ProtoBuf.Message.BaseProp prop) {
            hpAddLabel.text = GetAddStr(prop.perhp);
            attAddLabel.text = GetAddStr(prop.peratt);
            phyDefAddLabel.text = GetAddStr(prop.perpdef);
            magdefAddLabel.text = GetAddStr(prop.permdef);
            hitAddLabel.text = GetAddStr(prop.hit);
            dodgeAddLabel.text = GetAddStr(prop.dodge);
            criAddLabel.text = GetAddStr(prop.cri);
            antiCriAddLabel.text = GetAddStr(prop.antiCri);
            damageAddLabel.text = GetAddStr(prop.damageAdd);
            damageReduceLabel.text = GetAddStr(prop.damageReduce);
        }
        string GetAddStr(double value)
        {
            return string.Format("+{0}%", (value * 100).ToString("f0"));
        }

        GameObject currentSelectItem = null;
        public void OnAdditionItemClick(GameObject obj) {
            List<GameObject> items = new List<GameObject>();
            items.Add(threeStarItem.gameObject);
            items.Add(sixStarItem.gameObject);
            items.Add(nineStarItem.gameObject);
            foreach (var item in items) {
                if (item == obj)
                {
                    item.GetComponent<RushBuyAdditionItem>().Select();
                }
                else {
                    item.GetComponent<RushBuyAdditionItem>().UnSelect();
                }
            }
            currentSelectItem = obj;
        }

        public void OnConfirmButtonClick() {
            if (currentSelectItem == null) {
                Debug.LogError("Select!!!");
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("rush_please_chose_addition"));
                return;
            }
            if (curCostStarNum < currentSelectItem.GetComponent<RushBuyAdditionItem>().GetCostValue()) {
                Debug.LogError("No Enough Star!!!");
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("rush_star_lack"));
                return;
            }

            ProtoBuf.Message.CSRushAddition msg = new ProtoBuf.Message.CSRushAddition();
            msg.select = currentSelectItem.GetComponent<RushBuyAdditionItem>().GetAddition();
            msg.star = currentSelectItem.GetComponent<RushBuyAdditionItem>().GetCostValue();
            NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSRushAddition, msg, (pack, b) => {
                if (b)
                {
                    Close();

                }
            });
        }



    }

}